package com.qf.sl.youshare.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/7.
 * 16:39
 */
public class Category {

    private String id;
    private String name;
    private String url;
    private List<Category> subCategories;

    public Category(){
        subCategories = new LinkedList<Category>();
    }

    public void parseJSON(JSONObject jsonObject){
        if (jsonObject != null) {
            try {
                id = jsonObject.getString("id");
                name = jsonObject.getString("name");
                url = jsonObject.getString("url");

                try {

                    //获取子分类的内容,如果没有子分类,将会出现一个异常
                    //  我们忽略即可

                    JSONObject cat_id = jsonObject.getJSONObject("cat_id");
                    JSONArray names = cat_id.names();
                    if (names != null) {
                        int len = names.length();
                        for (int i = 0; i < len; i++) {
                            String string = names.getString(i);
                            JSONObject object = cat_id.getJSONObject(string);

                            Category category = new Category();
                            category.parseJSON(object);

                            subCategories.add(category);
                        }
                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public List<Category> getSubCategories() {
        return subCategories;
    }
}
